package com.jeewms.www.tms.util;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.jeewms.www.tms.R;
import com.jeewms.www.tms.ui.view.MyZLoadingDialog;
import com.jeewms.www.tms.ui.view.QRPopupView;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import static com.lxj.xpopup.enums.PopupAnimation.TranslateFromBottom;
import static com.zyao89.view.zloading.Z_TYPE.CIRCLE;

/**
 * Created by 13799 on 2019/7/24.
 */

public class UiHelper {


    public static int REQUEST_CODE_BROWSE_IMAGE = 100;
    public static int REQUEST_CODE_SELECTED_IMAGE = REQUEST_CODE_BROWSE_IMAGE + 1;
    public static int REQUEST_CODE_TAKE_PHOTO = REQUEST_CODE_BROWSE_IMAGE + 2;
    public static int REQUEST_CODE_CROP_IMAGE = REQUEST_CODE_BROWSE_IMAGE + 3;
    public static String RESULT_SELECTED_IMAGES = "SelectedImages";
    private static MyZLoadingDialog dialog;


    private static Toast toast;

    public static void showToast(Context context, int msgResId) {
        String msg = context.getString(msgResId);
        showToast(context, msg);
    }

    public static void showToast(Context context, String msg) {
        showToast(context, msg, 0);
    }

    public static void showToast(Context context, String msg, int drawableResId) {
        TextView tv = new TextView(context);
        tv.setBackgroundResource(R.drawable.toast_bg);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        tv.setPadding(24, 24, 24, 24);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        tv.setTextColor(context.getResources().getColor(R.color.app_toast_text_color));
        tv.setText(msg);
        if (drawableResId != 0) {
            tv.setCompoundDrawablePadding(20);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
                tv.setCompoundDrawablesWithIntrinsicBounds(drawableResId, 0, 0, 0);
            }
        }
        if (toast == null) {
            toast = new Toast(context);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setView(tv);
        } else {
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setView(tv);
        }
        toast.show();
    }

    public static void showToast(Context context, String msg, boolean isSuccese) {
        TextView tv = new TextView(context);
        tv.setBackgroundResource(R.drawable.toast_bg);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        tv.setPadding(24, 24, 24, 24);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        tv.setTextColor(context.getResources().getColor(R.color.app_toast_text_color));
        tv.setText(msg);
        if (isSuccese) {
            tv.setCompoundDrawablePadding(20);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
                tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ok, 0, 0, 0);
            }
        } else {
            tv.setCompoundDrawablePadding(20);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
                tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.error, 0, 0, 0);
            }
        }
        if (toast == null) {
            toast = new Toast(context);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setView(tv);
        } else {
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setView(tv);
        }
        toast.show();
    }

    public interface TakePhotoListener {
        public void onBeforeTakePhoto(boolean isThumb, File hdFile);
    }

    private static PopupWindow mPopupWindow;



    public static void dismissPopupDlg() {
        if (mPopupWindow != null )
            try {
                mPopupWindow.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    //    public static void setImage(File file, ImageView view, ProgressBar progress) {
//        OpenApi.loadImage(file, view, progress);
//    }
    public static void setImage(int resid, ImageView view, ProgressBar progress) {
        if (progress != null)
            progress.setVisibility(View.VISIBLE);
        view.setTag(resid);
        view.setImageResource(resid);
        if (progress != null)
            progress.setVisibility(View.GONE);
    }





    private static int getResizedDimension(int maxPrimary, int maxSecondary,
                                           int actualPrimary, int actualSecondary, ImageView.ScaleType scaleType) {

        // If no dominant value at all, just return the actual.
        if ((maxPrimary == 0) && (maxSecondary == 0)) {
            return actualPrimary;
        }

        // If ScaleType.FIT_XY fill the whole rectangle, ignore ratio.
        if (scaleType == ImageView.ScaleType.FIT_XY) {
            if (maxPrimary == 0) {
                return actualPrimary;
            }
            return maxPrimary;
        }

        // If primary is unspecified, scale primary to match secondary's scaling
        // ratio.
        if (maxPrimary == 0) {
            double ratio = (double) maxSecondary / (double) actualSecondary;
            return (int) (actualPrimary * ratio);
        }

        if (maxSecondary == 0) {
            return maxPrimary;
        }

        double ratio = (double) actualSecondary / (double) actualPrimary;
        int resized = maxPrimary;

        // If ScaleType.CENTER_CROP fill the whole rectangle, preserve aspect
        // ratio.
        if (scaleType == ImageView.ScaleType.CENTER_CROP) {
            if ((resized * ratio) < maxSecondary) {
                resized = (int) (maxSecondary / ratio);
            }
            return resized;
        }

        if ((resized * ratio) > maxSecondary) {
            resized = (int) (maxSecondary / ratio);
        }
        return resized;
    }
    private static int findBestSampleSize(int actualWidth, int actualHeight,
                                          int desiredWidth, int desiredHeight) {
        double wr = (double) actualWidth / desiredWidth;
        double hr = (double) actualHeight / desiredHeight;
        double ratio = Math.min(wr, hr);
        float n = 1.0f;
        while ((n * 2) <= ratio) {
            n *= 2;
        }
        return (int) n;
    }
    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 1000;

    private static final int MIN_CLICK_DELAY_TIME3 = 3000;
    private static final int MIN_CLICK_DELAY_TIME2= 500;
    private static long lastClickTime;

    public static boolean isFastClick1000() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }
    public static boolean isFastClick500() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME2) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }
    public static boolean isFastClick3000() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME3) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    public static void showBigQR(Context context, String str){
        QRPopupView qrPopupView=new QRPopupView(context,str);
        showPopup(context,qrPopupView);
    }

    private static void showPopup(Context context, BasePopupView view){
        new XPopup.Builder(context)
                .popupAnimation(TranslateFromBottom)
                .asCustom(view/*.enableDrag(false)*/)
                .show();
    }

    public static void showLoading(Context context) {
        dialog = new MyZLoadingDialog(context);
        dialog.setLoadingBuilder(CIRCLE)
                //设置类型
                .setLoadingColor(Color.GRAY)
                //颜色
                .setHintText("Loading...")
                .setCancelable(false)
                .show();

    }

    public static void hideLoading() {
        if (dialog != null&&dialog.isShow()) {
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    dialog.dismiss();
                }
            },1000);
        }
    }
}
